#ifndef SPLASH_SCREEN_H
#define SPLASH_SCREEN_H

#include <gtkmm.h>
#include <libglademm.h>

#include <string>

class SplashScreen{
	public:
		SplashScreen():info_label((Gtk::Label*)1){
			Gtk::Window::set_auto_startup_notification(false);
			
			//modify splash screen colors for asthetic appeal
			xml_interface=Gnome::Glade::Xml::create("ChatServer.glade", "splash_screen");
			xml_interface->get_widget("splash_screen", window);
			
			xml_interface->get_widget("eventbox1", eventbox);
			//change eventbox1 background color to white
			Gdk::Color color("white");
			eventbox->modify_bg (Gtk::STATE_NORMAL, color);
			//change eventbox2 background color to white
			xml_interface->get_widget("eventbox2", eventbox);
			eventbox->modify_bg (Gtk::STATE_NORMAL, color);
			window->show_all();
			
			//get the lable that will change during loading
			xml_interface->get_widget("info_label", info_label);
			if(!info_label){
				g_warning("info_label not found");
			}
			
			//connect the dispatcher so that GUI commands are called from one thread
			dispatcher.connect(sigc::mem_fun(*this, &SplashScreen::updateInfoText));
			Gtk::Window::set_auto_startup_notification(true);
			
		}
		virtual ~SplashScreen(){
			delete window;
		}
		void setInfoText(const std::string& s){
			g_debug("setInfoText:%s", s.c_str());
			info_label_text=s.c_str();
			dispatcher();
			while (Gtk::Main::events_pending()){
				Gtk::Main::instance()->iteration();
			}
		}
	protected:
		void updateInfoText(){
			static Gdk::Color color("white");
			g_debug("updateInfoText:%s", info_label_text.c_str());
			info_label->set_label (info_label_text);
			eventbox->modify_bg (Gtk::STATE_NORMAL, color);
			//info_label->show_now();
			window->show_all();
			g_thread_yield();
			g_usleep(G_USEC_PER_SEC*4/10);
		}
		
		Glib::RefPtr<Gnome::Glade::Xml> xml_interface;
		
		Gtk::Label* info_label;
		Gtk::EventBox* eventbox;
		Gtk::Window* window;
		
		Glib::Dispatcher dispatcher;
		Glib::ustring info_label_text;
};

#endif
